package com.example.LibrarySystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.LibrarySystem.mapper.BookMapper;
import com.example.LibrarySystem.mapper.InventoryMapper;
import com.example.LibrarySystem.pojo.Book;
import com.example.LibrarySystem.pojo.User;
import com.example.LibrarySystem.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public int insert(Book book) {
        return bookMapper.insert(book);
    }

    @Override
    public int update(Book book,Integer number) {
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.eq("id",book.getId());
        int update = bookMapper.update(book, bookQueryWrapper);

        int res = inventoryMapper.updateNumberById(number,book.getId());

        return update + res;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public Book getBookById(Integer id) {
        return null;
    }


    @Override
    public IPage<Book> getBookByAll(Map<String, String> data) {
        int start = data.containsKey("start") ? Integer.parseInt(data.get("start")) : 1;
        int pages = data.containsKey("pages") ? Integer.parseInt(data.get("pages")) : 10;

        IPage<Book> bookIPage= new Page<>(start, pages);

        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("soft_del",0);

        if (data.containsKey("name") && !"".equals(data.get("name"))) {
            queryWrapper.like("name", data.get("name"));
        }

        if (data.containsKey("pressName") && !"".equals(data.get("pressName"))) {
            queryWrapper.like("press_name", data.get("pressName"));
        }

        if (data.containsKey("bookClassId") && !"".equals(data.get("bookClassId"))) {
            queryWrapper.eq("book_class_id", Integer.parseInt(data.get("bookClassId")));
        }

        IPage<Book> bookIPage1 = bookMapper.selectPage(bookIPage, queryWrapper);

        return bookIPage1;
    }
}
